package com.wisdytech.linkdcs.equipLifeCycle.maintain.service.impl;

import com.wisdytech.linkdcs.equipLifeCycle.maintain.model.MTypeItem;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.model.MaintenanceItem;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.dao.IMTypeItemDao;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.dao.IMaintenanceItemDao;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.service.IMTypeItemService;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenbin
 * @since 2018-06-10
 */
@Service("iMTypeItemService")
public class MTypeItemServiceImpl extends BaseService<IMTypeItemDao, MTypeItem> implements IMTypeItemService {

	@Resource
	private IMTypeItemDao iMTypeItemDao;
	@Override
	public void addTypeAndItem(String typeId, String itemIds) {
        String[] id = itemIds.split(",");
//        for (int i = 0; i < id.length; i++) {
    		EntityWrapper<MTypeItem> ew = new EntityWrapper<>();
//            //验证类别是否被关联
//            List<MTypeItem> list = new ArrayList<>();
//            list = iMTypeItemDao.selectPage(new Page<Part>(1, 1), ew.eq("ITEM_ID", id[i]).eq("TYPE_ID", typeId));
//            if (!list.isEmpty()) {
//            	throw new ServiceException("所选记录中存在已关联保养项");
//            }
    		ew.eq("TYPE_ID", typeId);
        	iMTypeItemDao.delete(ew);
        	
//        }
        for (int i = 0; i < id.length; i++) {
        	MTypeItem mTypeItem = new MTypeItem();
        	mTypeItem.setPkId(BaseUtil.getUUID());
        	mTypeItem.setTypeId(typeId);
        	mTypeItem.setItemId(id[i]);
        	iMTypeItemDao.insert(mTypeItem);
        }
	}
	@Override
	public PageInfo<MaintenanceItem> listTypeItemByFilter(String typtCode,Filter filter) {
		EntityWrapper<MaintenanceItem> ew = new EntityWrapper<>();
		List<MaintenanceItem> list =super.beforeList(IMaintenanceItemDao.class, filter).selectList(ew.eq("del_flag", CommonConstants.DEL_FLAG));
		PageInfo<MaintenanceItem> page = new PageInfo<>(list);
		return page;
	}
	@Override
	public List<MTypeItem> listTypeItemByPkId(String pkId) {
		EntityWrapper<MTypeItem> ew = new EntityWrapper<>();
		ew.eq("TYPE_ID", pkId);
		List<MTypeItem> list = iMTypeItemDao.selectList(ew);
		return list;
	}
	@Override
	public PageInfo<MaintenanceItem> listTypeItemByTaskId(String taskId, Filter filter) {
		List<MaintenanceItem> list =super.beforeList(IMaintenanceItemDao.class, filter).listTypeItemByTaskId(taskId);
		PageInfo<MaintenanceItem> page = new PageInfo<>(list);
		return page;
	}
}
